package org.springblade.modules.repair.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.repair.entity.PlanHistoryImage;
import org.springblade.modules.repair.service.PlanHistoryImageService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Date 15:23 2024/6/4
 * @Description {
 *   工单图片AI处理结果
 * }
 * @Author lsay
 **/
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("repair/plan/image/history")
public class PlanImageHistoryController {

	private final PlanHistoryImageService planHistoryImageService;

	@GetMapping("/page")
	public R<IPage<PlanHistoryImage>> selectPage(PlanHistoryImage entity, Query query){
		return R.data(planHistoryImageService.selectPage(Condition.getPage(query),entity));
	}

	@GetMapping("/list")
	public R<List<PlanHistoryImage>> selectList(PlanHistoryImage entity){
		log.info("====================={ 检测代码版本 }========================");
		return R.data(planHistoryImageService.selectList(entity));
	}

	@GetMapping("/detail")
	public R<PlanHistoryImage> selectDetail(Long id,String url){
		if(Func.isEmpty(id) && Func.isEmpty(url)){
			throw new ServiceException("查询条件不可为空");
		}
		return R.data(planHistoryImageService.selectDetail(id,url));
	}

	@PutMapping("/submit")
	public R<Boolean> submit(@RequestBody PlanHistoryImage entity){
		return R.data(planHistoryImageService.saveOrUpdate(entity));
	}

	@PutMapping("/update/v2")
	public R<Boolean> updateV2(@RequestBody PlanHistoryImage entity){
		return R.data(planHistoryImageService.update(entity,Wrappers.<PlanHistoryImage>lambdaUpdate().eq(PlanHistoryImage::getUrl,entity.getUrl())));
	}

	@PutMapping("/update/appeal")
	public R<Boolean> updateAppeal(@RequestBody PlanHistoryImage entity){
		PlanHistoryImage image = planHistoryImageService.getById(entity.getId());
		if(Func.isEmpty(image)){
			throw new ServiceException("图片数据不存在");
		}else{
			if(image.getStatus() != 1){
				throw new ServiceException("图片已审核");
			}
		}
		entity.setAppealUser(AuthUtil.getUserId());
		entity.setAppealTime(DateUtil.date());
		return R.data(planHistoryImageService.updateById(entity));
	}

}
